package com.co.video.controller;

import com.co.common.entity.Result;
import com.co.video.pojo.Category;
import com.co.video.service.CategoryService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ldl.plus
 * @date 2020年06月22日  0:41
 */

@RestController
@RequestMapping("/video/category")

public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * GlmCategory分页条件搜索实现
     *
     * @param category
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<Category>> findPage(@RequestBody(required = false) Category category, @PathVariable int page, @PathVariable int size) {
        //调用GlmCategoryService实现分页条件查询GlmCategory
        PageInfo<Category> pageInfo = categoryService.findPage(category, page, size);
        return Result.ok(pageInfo);
    }

    /**
     * GlmCategory分页搜索实现
     *
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<Category>> findPage(@PathVariable int page, @PathVariable int size) {
        //调用GlmCategoryService实现分页查询GlmCategory
        PageInfo<Category> pageInfo = categoryService.findPage(page, size);
        return Result.ok(pageInfo);
    }

    /**
     * 多条件搜索品牌数据
     *
     * @param category
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<Category>> findList(@RequestBody(required = false) Category category) {
        //调用GlmCategoryService实现条件查询GlmCategory
        List<Category> list = categoryService.findList(category);
        return Result.ok(list);
    }

    /**
     * 根据ID删除品牌数据
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result<?> delete(@PathVariable String id) {
        //调用GlmCategoryService实现根据主键删除
        categoryService.delete(id);
        return Result.ok();
    }

    /**
     * 修改GlmCategory数据
     *
     * @param category
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result<?> update(@RequestBody Category category, @PathVariable String id) {
        //设置主键值
        category.setId(id);
        //调用GlmCategoryService实现修改GlmCategory
        categoryService.update(category);
        return Result.ok();
    }

    /**
     * 新增GlmCategory数据
     *
     * @param category
     * @return
     */
    @PostMapping
    public Result<?> add(@RequestBody Category category) {
        //调用GlmCategoryService实现添加GlmCategory
        categoryService.add(category);
        return Result.ok();
    }

    /**
     * 根据ID查询GlmCategory数据
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Category> findById(@PathVariable String id) {
        //调用GlmCategoryService实现根据主键查询GlmCategory
        Category category = categoryService.findById(id);
        return Result.ok(category);

    }

    /**
     * 查询GlmCategory全部数据
     *
     * @return
     */
    @GetMapping
    public Result<List<Category>> findAll() {
        //调用GlmCategoryService实现查询所有GlmCategory
        List<Category> list = categoryService.findAll();
        return Result.ok(list);
    }

    @RequestMapping("/find")
    public List<Category> findcategory() {
        return categoryService.findCategory();
    }

    @RequestMapping("/subfind/{id}")
    public List<Category> findSubCategory(@PathVariable String id) {
        return categoryService.findSubCategory(id);
    }
}
